package com.sokal.easycare.modules.picture.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.picture.entity.PicCategory;
import com.sokal.easycare.modules.picture.service.PicCategoryService;

/**
 * 图文栏目Controller
 * @author liliangdong
 * @version 2017-03-22
 */
@Controller
@RequestMapping(value = "${adminPath}/picture/picCategory")
public class PicCategoryController extends BaseController {

	@Autowired
	private PicCategoryService picCategoryService;
	
	@ModelAttribute
	public PicCategory get(@RequestParam(required=false) String id) {
		PicCategory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = picCategoryService.get(id);
		}
		if (entity == null){
			entity = new PicCategory();
		}
		return entity;
	}
	
	@RequiresPermissions("picture:picCategory:view")
	@RequestMapping(value = {"list", ""})
	public String list(PicCategory picCategory, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<PicCategory> list = picCategoryService.findList(picCategory); 
		model.addAttribute("list", list);
		return "modules/picture/picCategoryList";
	}

	@RequiresPermissions("picture:picCategory:view")
	@RequestMapping(value = "form")
	public String form(PicCategory picCategory, Model model) {
		if (picCategory.getParent()!=null && StringUtils.isNotBlank(picCategory.getParent().getId())){
			picCategory.setParent(picCategoryService.get(picCategory.getParent().getId()));
			// 获取排序号，最末节点排序号+30
			if (StringUtils.isBlank(picCategory.getId())){
				PicCategory picCategoryChild = new PicCategory();
				picCategoryChild.setParent(new PicCategory(picCategory.getParent().getId()));
				List<PicCategory> list = picCategoryService.findList(picCategory); 
				if (list.size() > 0){
					picCategory.setSort(list.get(list.size()-1).getSort());
					if (picCategory.getSort() != null){
						picCategory.setSort(picCategory.getSort() + 30);
					}
				}
			}
		}
		if (picCategory.getSort() == null){
			picCategory.setSort(30);
		}
		model.addAttribute("picCategory", picCategory);
		return "modules/picture/picCategoryForm";
	}

	@RequiresPermissions("picture:picCategory:edit")
	@RequestMapping(value = "save")
	public String save(PicCategory picCategory, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, picCategory)){
			return form(picCategory, model);
		}
		picCategoryService.save(picCategory);
		addMessage(redirectAttributes, "保存图文栏目成功");
		return "redirect:"+Global.getAdminPath()+"/picture/picCategory/?repage";
	}
	
	@RequiresPermissions("picture:picCategory:edit")
	@RequestMapping(value = "delete")
	public String delete(PicCategory picCategory, RedirectAttributes redirectAttributes) {
		picCategoryService.delete(picCategory);
		addMessage(redirectAttributes, "删除图文栏目成功");
		return "redirect:"+Global.getAdminPath()+"/picture/picCategory/?repage";
	}

	@RequiresPermissions("user")
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<PicCategory> list = picCategoryService.findList(new PicCategory());
		for (int i=0; i<list.size(); i++){
			PicCategory e = list.get(i);
			if (StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId()) && e.getParentIds().indexOf(","+extId+",")==-1)){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", e.getParentId());
				map.put("name", e.getName());
				mapList.add(map);
			}
		}
		return mapList;
	}
	
}